package ifmo.group5512.timer;

/**
 * Created with IntelliJ IDEA.
 * User: katerina
 * Date: 01.11.12
 * Time: 12:56
 * To change this template use File | Settings | File Templates.
 */
public class Commander {


    public static String processCommand(String str) {
        if (str.length() < 2) {
            return "-2";
        }

        Module module = Module.parse(str.substring(0, 2));
        if (module == null) {
            return "-1";
        }

        boolean result;
        if (str.contains("end")) {
            result = CurrentManagerConfiguration.self.stopWork(module);
        } else {
            result = CurrentManagerConfiguration.self.nextInterval(module);
        }
        if (result) {
            String answer = "1 " + CurrentManagerConfiguration.self.getIntervalIdx();
            if (module == Module.ModelingModule) {
                return answer + " " + (CurrentManagerConfiguration.self.getIntervalType() == IntervalType.InputData ? 0 : 1);
            }
            return answer;
        }
        return "0";
    }



    private static long parseManualInputDelay(String str) {
        int idx = str.indexOf(" ");
        if (idx == -1 || (idx + 1) >= str.length()) {
            System.err.println("Incorrect command format from front office.");
            return -1;
        }
        str = str.substring(idx + 1);

        try {
            return Long.parseLong(str);
        } catch (NumberFormatException ex) {
            System.err.println("Incorrect front office delay.");
            return -1;
        }
    }
}
